package labels;

import buttons.CommonButton;
import frames.AddFrame;
import frames.MainFrame;
import utils.PropertiesUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

/**
 * @version V1.0
 * @Title: 备忘的label
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class CommonLabel extends JLabel {

    public JLabel text = new JLabel("",JLabel.CENTER);
    public BarLabel barLabelLeft;
    public BarLabel barLabelRight;
    public JButton addBtn;
    public JButton editBtn;
    public JButton deleteBtn;
    public PropertiesUtil dataCache = new PropertiesUtil("DATA.properties");

    public CommonLabel(int y, int no) throws IOException {
        setBtn(no);
        setLabel(y);
    }

    public void addAddBtn(){
        this.add(addBtn);
    }

    public void addEditBtn(){
        this.add(editBtn);
    }

    public void addDeleteBtn(){
        this.add(deleteBtn);
    }

    public void deleteAddBtn(){
        this.remove(addBtn);
    }

    public void deleteAllBtn(){
        this.remove(addBtn);
        this.remove(editBtn);
        this.remove(deleteBtn);
        this.text.setText("");
    }

    private void setBtn(int no) throws IOException {
        BufferedImage addImg = ImageIO.read(new File("resources/images/add.png"));
        BufferedImage editImg = ImageIO.read(new File("resources/images/edit.png"));
        BufferedImage deleteImg = ImageIO.read(new File("resources/images/delete.png"));
        addBtn = new CommonButton(addImg,330,10,80,80);
        addBtn.addActionListener(new addBtn());
        editBtn = new CommonButton(editImg,20,10,80,80);
        editBtn.setName("" + no);
        editBtn.addActionListener(new editBtn());
        deleteBtn = new CommonButton(deleteImg,640,10,80,80);
        deleteBtn.setName("" + no);
        deleteBtn.addActionListener(new deleteBtn());
    }

    private void setLabel(int y) {
        this.setIcon(new ImageIcon("resources/images/label.png"));
        this.setFont(new Font("微软雅黑", Font.PLAIN, 38));
        this.setForeground(Color.WHITE);
        this.setBounds(20,y,740,100);
        text.setText("");
        text.setFont(new Font("微软雅黑", Font.PLAIN, 38));
        text.setForeground(Color.WHITE);
        text.setBounds(120,0,500,100);
        barLabelLeft = new BarLabel();
        barLabelRight = new BarLabel();
        barLabelRight.setBounds(730,5,5,90);
        this.add(text);
        this.add(barLabelLeft);
        this.add(barLabelRight);
    }

    private static class addBtn implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //创建新的窗口
            AddFrame addFrame = new AddFrame();
            int screenWidth= Toolkit.getDefaultToolkit().getScreenSize().width;
            int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
            //设置在屏幕的位置
            addFrame.setLocation((screenWidth-300)/2,(screenHeight-240)/2);
            //窗体大小
            addFrame.setSize(600,480);
            //显示窗体
            addFrame.setVisible(true);
        }

    }

    private class deleteBtn implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            deleteBtn();
        }

    }

    private class editBtn implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //创建新的窗口
            AddFrame editFrame = new AddFrame();
            int screenWidth= Toolkit.getDefaultToolkit().getScreenSize().width;
            int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
            //设置在屏幕的位置
            editFrame.setLocation((screenWidth-300)/2,(screenHeight-240)/2);
            //窗体大小
            editFrame.setSize(600,480);
            //显示窗体
            editFrame.setVisible(true);
            editBtn(editFrame);
        }

    }

    public void editBtn(AddFrame editFrame){
        String name = "data" + editBtn.getName();
        editFrame.textArea.setText(dataCache.get(name));
        editFrame.isUpdate = true;
        editFrame.signal = Integer.parseInt(editBtn.getName());
    }

    public void deleteBtn(){
        ArrayList<String> data = new ArrayList<>();
        setData(data);
        Properties cache = new Properties();
        setPropertys(cache);
        for (int i=0;i<5;i++){
            if (i>=Integer.parseInt(deleteBtn.getName()) && i<4){
                cache.setProperty("data"+i,dataCache.get("data"+(i+1)));
                dataCache.set("data"+i,dataCache.get("data"+(i+1)));
                MainFrame.dataCache.set("data"+i,dataCache.get("data"+(i+1)));
            }else if (i==4){
                cache.setProperty("data"+i,"0");
                dataCache.set("data"+i,"0");
                MainFrame.dataCache.set("data"+i,"0");
            }
        }
        dataCache.set(cache,"DATA.properties");
        MainFrame.deleteLabels();
        MainFrame.setLabels();
        MainFrame.refreshFrame();
    }

    public void setPropertys(Properties cache){
        cache.setProperty("data0",dataCache.get("data0"));
        cache.setProperty("data1",dataCache.get("data1"));
        cache.setProperty("data2",dataCache.get("data2"));
        cache.setProperty("data3",dataCache.get("data3"));
        cache.setProperty("data4",dataCache.get("data4"));
    }

    public void setData(ArrayList<String> data){
        data.add(dataCache.get("data0"));
        data.add(dataCache.get("data1"));
        data.add(dataCache.get("data2"));
        data.add(dataCache.get("data3"));
        data.add(dataCache.get("data3"));
    }
}
